// c:\Users\xiaodaiyan\Desktop\douyin-project\神椰抖音小程序\pages\my\my.js
import {
  customerInfo,
  putSetAvatar,
  putSetNickName,
  customerV1Logout
} from '../../api/my'
const handleAuthorize = require('../../utils/handleAuthorize')
Page({
  data: {
    orderStatusLs:[
      {id:'0',img:'/static/my/icon_1.png',value:'待使用',indexId:1},
      {id:'1',img:'/static/my/icon_2.png',value:'待付款',indexId:2},
      {id:'2',img:'/static/my/icon_3.png',value:'已完成',indexId:3},
      {id:'3',img:'/static/my/icon_4.png',value:'退款',indexId:5},
    ],
    toolLs:[
      {id:'0',img:'/static/my/icon_6.png',value:'积分攻略'},
      {id:'1',img:'/static/my/icon_7.png',value:'服务资质'},
      // {id:'3',img:'/static/my/icon_zj.png',value:'收藏足迹'},
      {id:'2',img:'/static/my/icon_8.png',value:'联系客服'},
      // {id:'4',img:'/static/my/ic_my_anchor.png',value:'主播列表'},
    ],
    creationLs:[
      {id:'0',img:'/static/my/icon_12.png',value:'我的作品'},
      {id:'1',img:'/static/my/icon_11.png',value:'我的收益'},
    ],
    isToken:false, //是否登录
    customer:{}, //用户详情
  },
  onLoad(){

  },
  onShow(){
    this.setData({
      isToken:handleAuthorize.validate_login()
    })
    if(this.data.isToken){
       this.getCustomer()
    }
  },
  //设置中心
  jumpSet(){
    tt.navigateTo({
      url: '/pages/subPackages/pagesB/setUp/setUp',
    });
  },
  onHide(){
    this.loginPopupClose()
  },
  // 取消登录
  cancelFunc(){

  },
  // 确认登录
  confirmFunc(){
    handleAuthorize.jumpPublicLogin()
  },
  //订单跳转
  jumpOrder(e){
    if(this.data.isToken){
      let item = e.currentTarget.dataset.item
      tt.navigateTo({
        url: '/pages/subPackages/pagesB/orderList/orderList?indexId='+item.indexId,
      });
    }else{
      const childComponent = this.selectComponent('#loginPopupId');
      childComponent.openPopup();
    }

  },
  // 全部订单
  jumpAllOrder(){
    if(this.data.isToken){
      tt.navigateTo({
        url: '/pages/subPackages/pagesB/orderList/orderList'
      });
    }else{
      const childComponent = this.selectComponent('#loginPopupId');
      childComponent.openPopup();
    }

  },
  // 获取用户信息
  async getCustomer() {
    var res = await customerInfo()
    this.setData({
      customer:res.data
    })
  },
  //工具列表跳转
  toolJump(e){
    let item = e.currentTarget.dataset.item
    if(item.id==0){
      tt.navigateTo({
        url: '/pages/subPackages/pagesB/integralStrategy/integralStrategy',
      });
    }else if(item.id==1){
      tt.navigateTo({
        url: '/pages/subPackages/pagesB/serveAptitude/serveAptitude',
      });
    }else if(item.id==3){
      if(this.data.isToken){
        tt.navigateTo({
          url: '/pages/subPackages/pagesB/collectList/collectList',
        });
      }else{
        const childComponent = this.selectComponent('#loginPopupId');
        childComponent.openPopup();
      }
    }else if(item.id==4){
      if(this.data.isToken){
        tt.navigateTo({
          url: '/pages/subPackages/pagesB/anchorList/anchorList',
        });
      }else{
        const childComponent = this.selectComponent('#loginPopupId');
        childComponent.openPopup();
      }
    }
  },
  //创作者中心跳转
  creationJump(e){
    let item = e.currentTarget.dataset.item
    if(item.id==0){
      tt.navigateTo({
        url: '/pages/subPackages/pagesB/workList/workList',
      });
    }else if(item.id==1){
      tt.navigateTo({
        url: '/pages/subPackages/pagesB/incomeList/incomeList',
      });
    }
  },
  // 跳转积分攻略
  jumpIntegralStrategy(){
    tt.navigateTo({
      url: '/pages/subPackages/pagesB/integralStrategy/integralStrategy',
    });
  },
  // 关闭登录弹窗
  loginPopupClose(){
    const childComponent = this.selectComponent('#loginPopupId');
    childComponent.closePopup();
  },
  // 退出登录
  loginOut(){
    const childComponent = this.selectComponent('#loginOut');
    childComponent.open();
  },
  // 暂不退出 登录
  doNotExitTemporarily(){
    const childComponent = this.selectComponent('#loginOut');
    childComponent.close();
  },
  // 狠心退出 登录
  async beRuthlessAndWithdraw(){
    try {
      var res = await customerV1Logout()
      tt.clearStorageSync() //同步清除本地缓存
      tt.showToast({
        title: '退出成功',
        icon:'success'
      });
      setTimeout(() => {
        tt.reLaunch({
          url: '/pages/login/login'
        })
      }, 1000)

    } catch (e) {
      //TODO handle the exception
    }
  },
  // // 打开弹窗
  // open(popupName) {
  //   const childComponent = this.selectComponent('#'+popupName);
  //   childComponent.openPopup();
  // },
  // // 关闭弹窗
  // close(popupName) {
  //   const childComponent = this.selectComponent('#'+popupName);
  //   childComponent.close();
  // },
  getUserInfo(e){
    var type = e.currentTarget.dataset.type
    var that = this
    tt.getUserProfile({
      success(res) {
        console.log("getUserProfile 调用成功：", res.userInfo);
        if(type == 1){
          that.chooseAvatar(res.userInfo.avatarUrl)
        }else{
          that.dialogInputConfirm(res.userInfo.nickName)
        }
      },
      fail(res) {
        console.log("getUserProfile 调用失败", res);
      },
    });
  },
  async chooseAvatar(e){
    tt.showLoading({
      title: '请求中',
      mask: true
   })
    try {
      var res = await putSetAvatar(e)
      tt.hideLoading({
        success: (res) => {
          tt.showToast({
            title: '修改成功',
            icon:'success'
          });
        }
      });
      this.getCustomer()
     
    } catch {
      // uni.hideLoading()
    }
  },
  async dialogInputConfirm(val) {
     tt.showLoading({
        title: '请求中',
        mask: true
     })
    try {
      var res = await putSetNickName(val)
      tt.hideLoading({
        success: (res) => {
          tt.showToast({
            title: '修改成功',
            icon:'success'
          });
        }
      });
      this.getCustomer()
    } catch {
      // uni.hideLoading()
    }
  },
  onSuccess(e) {
    console.log("跳转平台客服成功", e.detail);
  },
  onError(e) {
    console.log("拉起平台客服失败", e.detail);
  },
})